package com.cloud.workTest;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.Properties;

public class DruidPropertiesTest {
    public static void main(String[] args) throws Exception {
        //数据源配置
        Properties properties=new Properties();
        //通过当前类的class对象获取资源文件
        InputStream is = DruidPropertiesTest.class.getResourceAsStream("/druid.properties");
        properties.load(is);
        //返回的是DataSource，不是DruidDataSource
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);

        //获取连接
        Connection connection = dataSource.getConnection();

        //Statement接口
        Statement statement = connection.createStatement();
        String sql1 = "insert into USER (user_name, nick_name, password, img) values ('shenpei', 'peit', '666666', 'http://test.unionpay.gsrcu.com/QRC/oqr/')";
        statement.executeUpdate(sql1);

        //PreparedStatement接口
        String sql2 = "insert into USER (user_name, nick_name, password, img) values ('zhuyidong', 'dongdong', '666666', 'http://test.unionpay.gsrcu.com/QRC/oqr/')";
        PreparedStatement preparedStatement = connection.prepareStatement(sql2);
        preparedStatement.execute();

        //关闭连接
        connection.close();
    }
}
